﻿package mortal.game
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.postProcess.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.mvc.core.*;

    public class GameLayout extends Object
    {
        private var _stage:Stage;
        private var _resizeCount:int = 0;
        private var _isReSize:Boolean = false;
        private var _stageChangeW:Number = 1;
        private var _stageChangeH:Number = 1;
        private static var _instance:GameLayout;

        public function GameLayout()
        {
            this._stage = Global.stage;
            if (_instance != null)
            {
                throw new Error(" GameLayout 是单例 ");
            }
            return;
        }// end function

        public function get stageChangeW() : Number
        {
            return this._stageChangeW;
        }// end function

        public function get stageChangeH() : Number
        {
            return this._stageChangeH;
        }// end function

        public function init() : void
        {
            this._stage.addEventListener(Event.RESIZE, this.onResizeHandler);
            return;
        }// end function

        private function onResizeHandler(event:Event) : void
        {
            if (event.target is Stage)
            {
                if (this._isReSize == false)
                {
                    this._isReSize = true;
                    setTimeout(this.resize, 50);
                }
                var _loc_2:String = this;
                var _loc_3:* = this._resizeCount + 1;
                _loc_2._resizeCount = _loc_3;
            }
            return;
        }// end function

        public function resize(param1:Boolean = true) : void
        {
            Log.debug("场景resize:" + this._resizeCount);
            this._resizeCount = 0;
            this._isReSize = false;
            var _loc_2:* = int(this._stage.stageWidth * 0.5) * 2;
            var _loc_3:* = int(this._stage.stageHeight * 0.5) * 2;
            var _loc_4:int = 4096;
            _loc_2 = 4096;
            var _loc_4:int = 2048;
            _loc_3 = 2048;
            if (_loc_2 > 2048 || _loc_3 > 1024)
            {
                PostProcess.instance.forceNotToTexture = true;
            }
            else
            {
                PostProcess.instance.forceNotToTexture = false;
            }
            this._stageChangeW = _loc_2 / SceneRange.display.width;
            this._stageChangeH = _loc_3 / SceneRange.display.height;
            SceneRange.display.width = _loc_2;
            SceneRange.display.height = _loc_3;
            if (Game.mapInfo)
            {
                SceneRange.init(Game.mapInfo);
            }
            if (Game.scene.isInLockScene)
            {
                Game.scene.scrollRect = SceneRange.display;
            }
            else if (RolePlayer.instance.isInitInfo)
            {
                RolePlayer.instance.setCurrentPointImpl();
            }
            Game.scene.resetViewPort();
            MsgManager.resize();
            EffectManager.stageResize();
            LayerManager.uiLayer.stageResize();
            LayerManager.fullScreenLayer.stageResize();
            LayerManager.smallIconLayer.stageResize();
            LayerManager.windowLayer.resize(this._stageChangeW, this._stageChangeH);
            Dispatcher.dispatchEvent(new DataEvent(EventName.StageResize));
            return;
        }// end function

        public static function get instance() : GameLayout
        {
            if (_instance == null)
            {
                _instance = new GameLayout;
            }
            return _instance;
        }// end function

    }
}
